package co.yixiang.modules.system.service.impl;

import co.yixiang.modules.system.domain.Shop;
import cn.hutool.core.convert.Convert;
import co.yixiang.common.service.impl.BaseServiceImpl;
import lombok.AllArgsConstructor;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import co.yixiang.common.utils.QueryHelpPlus;
import co.yixiang.utils.ValidationUtil;
import co.yixiang.utils.FileUtil;
import co.yixiang.modules.system.service.ShopService;
import co.yixiang.modules.system.dto.ShopDto;
import co.yixiang.modules.system.dto.queryCriteria.ShopQueryCriteria;
import co.yixiang.modules.system.mapper.ShopMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author zf
* @date 2020-08-15
*/
@Service
@AllArgsConstructor
//@CacheConfig(cacheNames = "shop")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class ShopServiceImpl extends BaseServiceImpl<ShopMapper, Shop> implements ShopService {

    @Override
    //@Cacheable
    public Map<String, Object> queryAll(ShopQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<Shop> page = new PageInfo<>(queryAll(criteria));
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("content", Convert.toList(ShopDto.class, page.getList()));
        map.put("totalElements", page.getTotal());
        return map;
    }


    @Override
    //@Cacheable
    public List<Shop> queryAll(ShopQueryCriteria criteria){
        return baseMapper.selectList(QueryHelpPlus.getPredicate(Shop.class, criteria));
    }


    @Override
    public void download(List<ShopDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ShopDto shop : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("uuid 唯一码", shop.getUnique());
            map.put("类型，1代表自营，2代表第三方app", shop.getType());
            map.put("供应商ID type==2 时有效", shop.getProviderId());
            map.put("第三方唯一ID", shop.getThirdpartyId());
            map.put("类型 1001==商店 1002==加油、汽站 1003==充电站", shop.getCategory());
            map.put("商铺拥有者ID", shop.getOwnerId());
            map.put("商铺名称", shop.getName());
            map.put("别名，用于识别具体店铺", shop.getAlias());
            map.put("电话", shop.getPhone());
            map.put("位置", shop.getLocation());
            map.put("店铺招牌", shop.getSignboard());
            map.put("列表icon", shop.getIcon());
            map.put(" longitude",  shop.getLongitude());
            map.put(" latitude",  shop.getLatitude());
            map.put("评分", shop.getScore());
            map.put("折扣， 与 promotion 表一致", shop.getDiscount());
            map.put("负责人姓名", shop.getLeaderName());
            map.put("负责人身份证", shop.getLeaderIdentiy());
            map.put("负责人身份证图片地址", shop.getLeaderIdentiyUrl());
            map.put("负责电话", shop.getLeaderPhone());
            map.put("营业执照地址", shop.getBlUrl());
            map.put("开户行", shop.getAccountName());
            map.put("开户账号", shop.getAccountNumber());
            map.put("创建日期", shop.getCreateDate());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
